package db

import (
	mydb "FILESTORE-SERVER/db/mysql"
	"fmt"
)

// 使用预编译的sql仅需sql操作 文件上传完成
func OnFileUploadFinished(filehash string, filename string,
	filesize int64, fileaddr string) bool {
	stmt, err := mydb.DBConn().Prepare(
		"insert ignore into tbl_file(`file_sha1`,`file_name`,`file_size`," +
			"`file_addr`, `status`) values (?,?,?,?,1)")
	defer stmt.Close()
	if err != nil {
		fmt.Println("Failed to prepare statement, err :" + err.Error())
		return false
	}
	ret, err := stmt.Exec(filehash, filename, filesize, fileaddr)
	if err != nil {
		fmt.Println("failed to exec db operation", err.Error())
		return false
	}
	// 返回插入数据库的结果
	if rf, err := ret.RowsAffected(); nil == err {
		if rf <= 0 {
			fmt.Printf("File with hash: %s has been uploaded before", filehash)
		}
		return false
	}
	return true
}
